/*
 * @Description: 客户订单属性详情DS
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2020-12-29 10:18:33
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-09 11:15:14
 */
import { DataSet } from 'choerodon-ui/pro/lib';
import { DataSetSelection, FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import commonConfigStore from '@common/config/commonConfig';

import intl from 'utils/intl';

const O2_OM = process.env.NODE_ENV === 'development' ? '/o2om-c' : commonConfigStore.O2_OM;

// const showOption = new DataSet({
//     selection: 'single' as DataSetSelection,
//     data: [
//         { text: intl.get('dji.otc.om.view.field.display').d('显示'), value: 1 },
//         { text: intl.get('dji.otc.om.view.field.shield').d('不显示'), value: 0 },
//     ],
// });



const isOption = new DataSet({
    selection: 'single' as DataSetSelection,
    data: [
        { text: intl.get('hzero.common.status.yes').d('是'), value: 1 },
        { text: intl.get('hzero.common.status.no').d('否'), value: 0 },
    ],
});


// const isDjiOption = new DataSet({
//     selection: 'single' as DataSetSelection,
//     data: [
//         { text: intl.get('hzero.common.status.yes').d('是'), value: 1 },
//         { text: intl.get('hzero.common.status.no').d('否'), value: 2 },
//         { text: intl.get('dji.otc.om.view.field.toBeConfirmedBySales').d('待销售确认'), value: 0 },
//     ],
// });
// const orderTypeOptions = new DataSet({
//     selection: 'single' as DataSetSelection,
//     autoQuery: true,
//     paging: false,
//     transport: {
//         read: config => {
//             return {
//                 ...config,
//                 url: `/hitf/v1/dji-external-interface/get-order-type`,
//                 data: [],
//                 method: 'post',
//                 transformResponse: res => {
//                     return JSON.parse(res).data;
//                 },
//             };
//         },
//     },
// });

const returnOption = new DataSet({
    selection: DataSetSelection.multiple,
    autoQuery: true,
    parentField: 'parentId',
    idField: 'id',
    transport: {
        read: config => {
            return {
                ...config,
                url: `/hpfm/v1/0/lovs/data?lovCode=DJIOM.RETURN_WAREHOUSE`,
                method: 'get',
                transformResponse: value => {
                    const reqData = JSON.parse(value);


                    const parentList: any = [];
                    // 去重的obj
                    const obj = {};
                    reqData.forEach((item) => {
                        // 给每条数据添加parentId
                        // eslint-disable-next-line no-param-reassign
                        item.id = item.value;
                        // eslint-disable-next-line no-param-reassign
                        item.parentId = item.parentValue;

                        const key = item.parentValue;
                        if (!obj[key]) {
                            parentList.push({
                                id: key,
                                meaning: key,
                            });
                            obj[key] = key;
                        }
                    });
                    const newList = reqData.concat(parentList);
                    console.log(newList);
                    // =================================改成children嵌套数据======================================
                    // reqData.forEach((item) => {
                    //     // 给每条数据添加label
                    //     // eslint-disable-next-line no-param-reassign
                    //     item.label = item.meaning;
                    //     const key = item.parentValue;
                    //     if (!obj[key]) {
                    //         parentList.push({
                    //             value: key,
                    //             label: key,
                    //             children: [],
                    //         });
                    //         obj[key] = key;
                    //     }
                    // });

                    // // 封装数据
                    // parentList.forEach((parentItem) => {
                    //     reqData.forEach((childrenItem) => {
                    //         if (parentItem.value === childrenItem.parentValue) {
                    //             parentItem.children.unshift(childrenItem)
                    //         }
                    //     })
                    // })
                    return newList;
                },
            };
        },
    },
});

// 订单属性DS
const customerOrderAttributeDtlDS = () => ({
    autoQuery: false,
    autoCreate: true,
    selection: 'multiple',
    primaryKey: 'id',
    paging: false,
    transport: {
        read: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/customer-order-configs/customize-detail`,
                method: 'get',
            };
        },
        submit: config => {
            return {
                ...config,
                data: config.data[0],
                url: `${O2_OM}/v1/customer-order-configs`,
                method: 'post',
            };
        },
    },
    fields: [
        {
            name: 'tradeName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.tradeName').d('交易身份'),
        },
        {
            name: 'groupName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.groupCustomerName').d('组别客户'),
        },
        {
            name: 'trulyName',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.customerTrulyName').d('注册客户'),
        },
        {
            name: 'tradeCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.tagTypeB').d('交易身份ID'),
        },
        {
            name: 'groupCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.isStatementApplied').d('组别客户ID'),
        },
        {
            name: 'trulyCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.isCreditApplied').d('客户注册ID'),
        },
        // {
        //     name: 'applicableDimensionCode',
        //     type: FieldType.string,
        //     label: intl.get('dji.otc.om.view.field.tagName').d('适用维度'),
        //     lookupCode: 'DJIOM.APPLICABLE_DIMENSION',
        //     required: true,
        //     defaultValue: 'group',
        // },
        {
            name: 'isIssueNumber',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.isIssueNumber').d('是否显示番号'),
            lookupCode: 'DJIOM.CUSTOMER_ORDER_DISPLAY',
        },
        {
            name: 'isPoNumber',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.isPoNumber').d('CI上是否显示PO No'),
            required: true,
            lookupCode: 'DJIOM.CUSTOMER_ORDER_DISPLAY',
        },
        {
            name: 'isModelNumber',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.isModelNumber').d('CI上是否显示Model No'),
            required: true,
            lookupCode: 'DJIOM.CUSTOMER_ORDER_DISPLAY',
        },
        {
            name: 'isDjiCustomsClearance',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.isDjiCustomsClearance').d('是否DJI清关'),
            required: true,
            lookupCode: 'DJIOM.IS_DJI_CUSTOMS_CLEARANCE'
        },
        {
            name: 'isSpecialFormat',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.isSpecialFormat').d('是否特殊CI/PL格式'),
            options: isOption,
            required: true,
            textField: 'text',
            valueField: 'value',
            defaultValue: 0,
        },
        {
            name: 'destinationCountryCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.destinationCountryCode').d('目的国优先级'),
            lookupCode: 'DJIOM.DESTINATION_COUNTRY',
            required: true,
            defaultValue: 'INVOICE_ADDRESS',
        },
        {
            name: 'isLocalWarehouse',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.isLocalWarehouse').d('是否本地仓发货'),
            options: isOption,
            required: true,
            textField: 'text',
            valueField: 'value',
            defaultValue: 0,
        },
        {
            name: 'local',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.tagTypeB').d('本地仓及贸易术语'),
        },
        // 订单类型
        {
            name: 'customerOrderTypeList',
            type: FieldType.object,
            required: true,
            // options: orderTypeOptions,
            textField: 'name',
            valueField: 'id',
            transformResponse: (value) => {
                const customerOrderTypeList: any = [];
                const data = value || [];
                data.forEach((item: any) => {
                    customerOrderTypeList.push(item.orderTypeCode);
                });
                return customerOrderTypeList;
            },
        },
        {
            name: 'orderTypeAll',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.orderTypeName').d('订单类型'),
            ignore: 'always',
        },
        // 核销类型
        {
            name: 'customerVerificationItemList',
            type: FieldType.object,
            // required: true,
            // lookupCode: 'DJIOM.VERIFICATION_ITEM',
        },
        {
            name: 'verificationAll',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.customerVerificationItemList').d('核销展示项'),
            ignore: 'always',
        },
        // 退货原因
        {
            name: 'isReturn',
            type: FieldType.number,
            label: intl.get('dji.otc.om.view.field.isReturn').d('是否允许退货'),
            options: isOption,
            required: true,
            textField: 'text',
            valueField: 'value',
            defaultValue: 0,
        },
        {
            name: 'returnReasonCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.returnReason').d('退货原因'),
            lookupCode: 'DJIOM.RETURN_REASON',
        },
        {
            name: 'reasonList',
            // type: FieldType.string,
            valueField: 'id',
            textField: 'meaning',
            options: returnOption,
            label: intl.get('dji.otc.om.view.field.reasonList').d('库存组织/子库'),
        },
    ],
});

// 订单类型DS
// const orderTypeDS = () => ({
//     autoQuery: false,
//     autoCreate: true,
//     selection: 'multiple',
//     primaryKey: 'id',
//     paging: false,
//     transport: {
//         read: config => {
//             return {
//                 ...config,
//                 url: `${O2_OM}/v1/customer-order-types`,
//                 method: 'get',
//                 transformResponse: (res) => {
//                     const customerOrderTypeList: any = [];
//                     const data = res ? JSON.parse(res) : [];
//                     data.forEach((item: any) => {
//                         customerOrderTypeList.push(item.orderTypeCode);
//                     });
//                     return { customerOrderTypeList };
//                 },
//             };
//         },
//     },
//     fields: [
//         {
//             name: 'customerOrderTypeList',
//             type: FieldType.object,
//             required: true,
//             // options: orderTypeOptions,
//             textField: 'name',
//             valueField: 'id',
//         },
//         {
//             name: 'orderTypeAll',
//             type: FieldType.string,
//             label: intl.get('dji.otc.om.view.field.orderType').d('订单类型'),
//         },
//     ],
// });

// 核销展示DS
// const verificationItemDS = () => ({
//     autoQuery: false,
//     selection: 'multiple',
//     primaryKey: 'id',
//     paging: false,
//     transport: {
//         read: config => {
//             return {
//                 ...config,
//                 url: `${O2_OM}/v1/customer-verification-items`,
//                 method: 'get',
//                 transformResponse: (res) => {
//                     const customerVerificationItemList: any = [];
//                     const data = res ? JSON.parse(res) : [];
//                     data.forEach((item: any) => {
//                         customerVerificationItemList.push(item.verificationItemCode);
//                     });
//                     return { customerVerificationItemList };
//                 },
//             };
//         },
//     },
//     fields: [
//         {
//             name: 'customerVerificationItemList',
//             type: FieldType.object,
//             // required: true,
//             // lookupCode: 'DJIOM.VERIFICATION_ITEM',
//         },
//         {
//             name: 'verificationAll',
//             type: FieldType.string,
//             label: intl.get('dji.otc.om.view.field.orderType').d('核销展示项'),
//         },
//     ],
// });

// 退货原因DS
const returnReasonDS = () => ({
    paging: false,
    selection: false,
    primaryKey: 'id',
    transport: {
        read: config => {
            return {
                ...config,
                url: `${O2_OM}/v1/customer-return-reasons`,
                method: 'get',
                transformResponse: (value) => {
                    const data = JSON.parse(value);
                    data.forEach((item) => {
                        // eslint-disable-next-line no-param-reassign
                        item.reasonList = [item.orgCode, item.warehouseCode];
                    });
                    return data;
                },
            };
        },
    },
    fields: [
        // {
        //     name: 'customerReturnReasonList',
        //     type: FieldType.string,
        //     label: '退货原因/库存组织/子库',
        // },
        {
            name: 'returnReasonCode',
            type: FieldType.string,
            label: intl.get('dji.otc.om.view.field.returnReason').d('退货原因'),
            lookupCode: 'DJIOM.RETURN_REASON',
        },
        {
            name: 'reasonList',
            // type: FieldType.string,
            valueField: 'id',
            textField: 'meaning',
            options: returnOption,
            label: intl.get('dji.otc.om.view.field.reasonList').d('库存组织/子库'),
        },
        // {
        //     name: 'orgCode',
        //     type: FieldType.string,
        //     label: intl.get('dji.otc.om.view.field.orderType').d('库存组织'),
        //     lookupCode: 'DJIOM.RETURN_ORG',
        // },
        // {
        //     name: 'warehouseCode',
        //     type: FieldType.string,
        //     label: intl.get('dji.otc.om.view.field.orderType').d('子库'),
        //     lookupCode: 'DJIOM.RETURN_WAREHOUSE',
        // },
    ],
});

export { customerOrderAttributeDtlDS, returnReasonDS };
